﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.SharePoint;
using NUnit.Framework;
using SharePoint.Orm.SiteBuilders;
using SharePoint.Orm.TestHelpers;
using SPGenerated;

namespace SharePoint.Orm.SiteBuilderTest {
    public abstract class TestBuilderBase : EntityBuilderBase {
        protected TestBuilderBase(BuilderContext builderContext) : base(builderContext) {
        }
        protected SiteFieldBuilder MySiteFieldBuilder { get { return (SiteFieldBuilder)SiteFieldBuilder; } }
    }
    public class ScoreBuilder : TestBuilderBase {
        public ScoreBuilder(BuilderContext builderContext)
            : base(builderContext) {
        }

        protected override ContentTypeBuilder GetContentTypeBuilder() {
            return new ContentTypeBuilder {
                Name = "Score",
                FieldLinks = new List<FieldLinkBuilder> { new FieldLinkBuilder { Field = MySiteFieldBuilder.TestField } }
            };
        }

        protected override ListBuilder GetListBuilder() {
            return new ListBuilder {
                Name = "Scores"
            };
        }

        protected override IEnumerable<ViewBuilder> GetViewBuilders() {
            return new[] { new ViewBuilder(), };
        }
    }
    public class SiteFieldBuilder : IFieldBuilder {
        public SPField TestField { get; set; }
        public void Build(SPDataContext ctx) {
            TestField = new FieldBuilder {
                GuidStr = "43DF71E3-C1A3-46DF-8CA2-7E39F3A63673",
                Name = "TestField"
            }.FindAdd();
        }
    }
    public class BuildingContainer : IBuildingContainer {
        public ScoreBuilder ScoreBuilder { get; set; }
    }
    [TestFixture]
    public class SuccessfulEntityCreation {

        [Test]
        public void TestSuccessfulCreationOfEntities() {
            using (var ctx = new MySPDataContext(SiteHelpers.Url)) {
                EntitiesBuilder builder = new EntitiesBuilder(ctx, new BuildingContainer(), new SiteFieldBuilder(),"TestGroup");
                builder.Build();
            }
            using(SPSite siteCollection = new SPSite(SiteHelpers.Url)) {
                using(SPWeb site = siteCollection.OpenWeb()) {
                    SPList list = site.Lists["Scores"];
                    Assert.IsNotNull(list);
                    SPField field = list.Fields["TestField"];
                    Assert.IsNotNull(field);
                    SPContentType contentType = list.ContentTypes["Score"];
                    Assert.IsNotNull(contentType);
                }
            }
        }
    }
}
